//////////////////////////////////////////////////
// EASIM project
// easim.win.FieldPane
// 6/7/2007 6:39 PM
// Cong Liu
// gzcong@gmail.com

// Copyright (C) 2007
// All rights reserved.
// Refer to LICENSE of JIST (Java In Simulation Time) for terms and conditions of use.

package easim.win;

import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.HashSet;

import easim.net.Field;
import easim.net.Node;

public class FieldPane extends JPanel implements jist.runtime.JistAPI.DoNotRewrite
{

  public NodeFactory nodeFactory;

  public Controler controler;

	public FieldPane() {
    controler = new EditControler(this);
		this.setupMouseEventHandlers();
		new RepaintThread().start();
	}

  class MouseListener1 implements MouseListener, jist.runtime.JistAPI.DoNotRewrite
  {
    public void mouseClicked(MouseEvent e) {
    }
    public void mouseEntered(MouseEvent e) {
    }
    public void mouseExited(MouseEvent e) {
    }
    public void mousePressed(MouseEvent e) {
      if (e.getButton() == MouseEvent.BUTTON1) {
        controler.mousePressed1(e.getX(), e.getY());
      }
      else if (e.getButton() == MouseEvent.BUTTON2) {
        controler.mousePressed2(e.getX(), e.getY());
      }
      else if (e.getButton() == MouseEvent.BUTTON3) {
        controler.mousePressed3(e.getX(), e.getY());
      }
    }
    public void mouseReleased(MouseEvent e)	 {
      controler.mouseReleased(e.getX(), e.getY());
    }	
  }

  class MouseMotionListener1 implements MouseMotionListener, jist.runtime.JistAPI.DoNotRewrite
  {
    public void mouseDragged(MouseEvent e) {
      controler.mouseDragged(e.getX(), e.getY());
    }
    public void mouseMoved(MouseEvent e) {
      controler.mouseMoved(e.getX(), e.getY());
    }
  }

	private void setupMouseEventHandlers() {
		this.addMouseListener(new MouseListener1());
		this.addMouseMotionListener(new MouseMotionListener1());
	}
	
	class RepaintThread extends Thread implements jist.runtime.JistAPI.DoNotRewrite
	{
		public synchronized void run() {
			this.setPriority(Thread.MIN_PRIORITY);
			while (true) {
				try {
          repaint();
					this.wait(Frame.REPAINT_INTERVAL);
				} catch (InterruptedException ex) {
				}
			}
		}
	}

	public synchronized void paint(Graphics graphics) {
		this.controler.paint(graphics);
	}

}
